home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_07 / 9n07044a < prev    next >
Text File  |  1991-05-08  |  2KB  |  109 lines

  1. include    file;
  2. include    command;
  3.  
  4. main:    entry    () =
  5.     {
  6.     fp:        stream;        // Input file
  7.     c:        int;
  8.     i:        int;
  9.     tabcolumn:    int;        // Column for tabs
  10.     lineno:        unsigned;    // Line # in file
  11.  
  12.     if    (ArgumentCount != 1){
  13.         stderr printf("Use is: PR filename\n");
  14.         return;
  15.         }
  16.     Printer newFile(getNextArgument());
  17.     i = fp open(Printer.filename, AR_READ);
  18.     if    (i != 0){
  19.         stderr printf("Couldn't open %s\n", 
  20.                     Printer.filename);
  21.         return;
  22.         }
  23.     Printer header();
  24.     lineno = 1;
  25.     while    ((c = fp getc()) != EOF){
  26.         printf("%5d ", lineno);
  27.         lineno++;
  28.         tabcolumn = 0;
  29.         while    (c != '\n' &&
  30.              c != '\f' &&
  31.              c != EOF){
  32.             if    (c == '\t'){
  33.                 i = padAmount(tabcolumn);
  34.                 tabcolumn += i;
  35.                 while    (i){
  36.                     stdout putc(' ');
  37.                     i--;
  38.                     }
  39.                 }
  40.             else    {
  41.                 stdout putc(c);
  42.                 tabcolumn++;
  43.                 }
  44.             c = fp getc();
  45.             }
  46.         if    (c == '\f')
  47.             Printer.lineCount = 1000;
  48.                         /* Force an
  49.                            end of
  50.                            page */
  51.         Printer endofline();
  52.         }
  53.     stdout putc('\f');            /* Finish
  54.                            the last
  55.                            page */
  56.     fp close();
  57.     }
  58.  
  59. padAmount:    (tabcolumn: int) int =
  60.     {
  61.     i:    int;
  62.  
  63.     i = (tabcolumn + 8) & 7;    /* compute the
  64.                        column within
  65.                        the tab */
  66.     return 8 - i;            /* spaces to pad */
  67.     }
  68.  
  69. Printer:    {
  70.  
  71. private:
  72.  
  73. pageCount:    int;            /* The current page
  74.                        number */
  75.  
  76. public:
  77.  
  78. lineCount:    int;            /* The current
  79.                        number of lines
  80.                        printed
  81.                        on a page */
  82. filename:    * char;            // Input file name
  83.  
  84. newFile:    (f: * char) =
  85.     {
  86.     filename = f;
  87.     pageCount = 1;
  88.     }
  89.  
  90. endofline:    () =
  91.     {
  92.     lineCount++;
  93.     if    (lineCount < 60)    // 60 lines per page
  94.         stdout putc('\n');
  95.     else    {
  96.         stdout putc('\f');
  97.         header();
  98.         }
  99.     }
  100.  
  101. header:    () =
  102.     {
  103.     printf("%-16s  page %d\n\n\n", filename, 
  104.                     pageCount++);
  105.     lineCount = 3;
  106.     }
  107.  
  108. };
  109.